
#include "RingQueue.hpp"
#include "Thread.hpp"
#include <string>
#include <vector>
#include <unistd.h>
#include <ctime>

using namespace ThreadModule;
int a = 10;
using ringqueue_t = RingQueue<int>;


void Consumer(ringqueue_t &rq)
{
    while (true)
    {
        
        std::cout << "Consumer..." << std::endl;
        sleep(1);
    }
}


void Productor(ringqueue_t &rq)
{
 
    srand(time(nullptr) ^ pthread_self());
    int cnt = 10;
    while (true)
    {
       
        std::cout << "Productor..." << std::endl;
        sleep(1);
    }
}


void StartComm(std::vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq, func_t<ringqueue_t> func)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "thread-" + std::to_string(i + 1);
        threads->emplace_back(func, bq, name);
        threads->emplace_back(func, rq, name);
        threads->back().Start();
    }
}

void StartConsumer(std::vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq)
{
    StartComm(threads, num, rq, Consumer);
}

void StartProductor(std::vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq)
{
    StartComm(threads, num, rq, Productor);
}

void WaitAllThread(std::vector<Thread<ringqueue_t>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}

int main()
{
    ringqueue_t *rq = new ringqueue_t(10);
    std::vector<Thread<ringqueue_t>> threads;
    // std::vector<Thread<ThreadData>> threads;

    StartProductor(&threads, 4, *rq);
    StartConsumer(&threads, 5, *rq);
    WaitAllThread(threads);

    return 0;